package dynamic.division;

/**
 * @Classname : UniqueBinarySearchTrees
 * @Description : 96. 不同的二叉搜索树
 * https://leetcode.cn/problems/unique-binary-search-trees/
 * @Author : cty
 * @Date 2023/3/28 22:15
 */


public class UniqueBinarySearchTrees {
    public int numTrees(int n) {
        int[] G = new int[n + 1];
        G[0] = 1;
        G[1] = 1;

        // 动态规划，先从n=2开始直至n=n
        for (int i = 2; i <= n; i++) {
            // 以j为根节点，两边分解成子问题
            for (int j = 1; j <= i; j++) {
                G[i] += G[j - 1] * G[i - j];
            }
        }
        return G[n];
    }
}
